﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MsForgeReboot.Framework.Entities;
using MsForgeReboot.Framework.Interfaces;

namespace MsForgeReboot.Framework.TownCrierChannels {
    public class BlogChannel : IChannel {

        private readonly IBlogService _service;

        public BlogChannel(IBlogService s) {
            this._service = s;
        }

        #region IChannel Members

        public Entities.Event Source { get; set; }

        public Entities.DeliveryResult DeliverEvent(Entities.Event source) {
            DeliveryResult result = new DeliveryResult();
            BlogPost post = new BlogPost {
                Id = source.Id,
                Body = source.Body,
                Published = source.Published,
                Title = source.Title,
                UrlPointer = source.UrlPointer,
                Author = source.Author,
            };
            try {
                this._service.Save(post);
                result.Success = true;
            } catch (Exception e) {
                result.Success = false;
                result.Message = e.Message;
            }
            result.DeliveredOn = DateTime.UtcNow;
            result.DeliveryChannel = "BlogChannel";
            return result;
        }

        public Entities.DeliveryResult DeliverEvent() {
            return this.DeliverEvent(this.Source);
        }

        #endregion
    }
}
